package com.cwt_taxi_xa;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.cwt_taxi_xa.db.bll.LocalInfoBiz;
import com.cwt_taxi_xa.model.LocalInfo;
import com.cwt_taxi_xa.util.DecimalUtil;
import com.cwt_taxi_xa.util.Tools;

import java.math.BigDecimal;
import java.util.ArrayList;

public class BusinessCount extends CWTBaseActivity implements OnClickListener,
		OnTouchListener{

	private static final String TAG = "QrOrderCount";
	public static Handler handler;
	private Button btn_today,btn_list;
	private TextView tv_amount_1day,tv_amount_7day,tv_count_1day,tv_count_7day;
	private LocalInfoBiz infoBiz;
	private ArrayList<LocalInfo> infosToday;
	private ArrayList<LocalInfo> infos7days;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.business_count);

		createHandler();
		setupViews();
		addListener();
		initDate();
	}

	private void createHandler() {
		// TODO Auto-generated method stub
		handler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				if(msg.what == 101){
				}
			};
		};
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		tv_amount_1day = (TextView) findViewById(R.id.tv_amount_1day);
		tv_amount_7day = (TextView) findViewById(R.id.tv_amount_7day);
		tv_count_1day = (TextView) findViewById(R.id.tv_count_1day);
		tv_count_7day = (TextView) findViewById(R.id.tv_count_7day);
		btn_today = (Button) findViewById(R.id.btn_today);
		btn_list = (Button) findViewById(R.id.btn_list);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		btn_list.setOnClickListener(this);
		btn_today.setOnClickListener(this);
	}

	/**
	 * 初始化数据
	 */
	public void initDate(){
		infoBiz = LocalInfoBiz.getInstance();
		//当日汇总
		infosToday = infoBiz.getTodayBusinessInfos();
		if(infosToday != null && infosToday.size() != 0){
			tv_count_1day.setText(""+infosToday.size());

			double amount = 0;
			for(int i = 0;i<infosToday.size();i++){
				LocalInfo info = infosToday.get(i);
				String data = info.getData();
				byte[] dataByte = Tools.yxReversal(Tools.parseHexStr2Byte(data));
				int amountIndex = 13 + 25 + 25 + 15 + 58;

				BigDecimal decimal = new BigDecimal(Tools.bcd2Str(Tools.byteTobyte(dataByte, amountIndex, 3)));
				decimal = decimal.divide(new BigDecimal(10));
				double subAmount = decimal.doubleValue();
				amount += subAmount;
			}

			tv_amount_1day.setText(DecimalUtil.priceFormat(amount));
		}else{
			tv_amount_1day.setText("0");
			tv_count_1day.setText("0");
		}

		// 7天汇总
		infos7days = infoBiz.get7daysBusinessInfo();
		if(infos7days != null && infos7days.size() != 0){
			tv_count_7day.setText(""+infos7days.size());

			double amount = 0;
			for(int i = 0;i<infos7days.size();i++){
				LocalInfo info = infos7days.get(i);
				String data = info.getData();
				byte[] dataByte = Tools.yxReversal(Tools.parseHexStr2Byte(data));

				int amountIndex = 13 + 25 + 25 + 15 + 58;

				BigDecimal decimal = new BigDecimal(Tools.bcd2Str(Tools.byteTobyte(dataByte, amountIndex, 3)));
				decimal = decimal.divide(new BigDecimal(10));
				double subAmount = decimal.doubleValue();
				amount += subAmount;
			}

			tv_amount_7day.setText(DecimalUtil.priceFormat(amount));
		}else{
			tv_amount_7day.setText("0");
			tv_count_7day.setText("0");
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler = null;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch (id) {
			case R.id.btn_today:
				startActivity(new Intent(BusinessCount.this, BusinessDataOneDay.class));
				break;
			case R.id.btn_list:
				startActivity(new Intent(BusinessCount.this, BusinessDataList.class));
				break;
		}
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
